package livesun.justiceeternal.adapter;
import android.text.format.DateUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import java.util.List;
import livesun.justiceeternal.R;
import livesun.justiceeternal.base.JEApp;
import livesun.justiceeternal.db.love.LoveBean;
import zlc.season.practicalrecyclerview.AbstractViewHolder;

public class LoveViewHolder extends AbstractViewHolder<LoveBean> {

    private final TextView title;
    private final TextView dateTv;
    private List<LoveBean> loveBeans;
    private final CheckBox checkBox;
    private boolean modle;


    public LoveViewHolder(ViewGroup parent) {
        super(parent, R.layout.love_item_list);
        title = itemView.findViewById(R.id.title);
        dateTv = itemView.findViewById(R.id.date);
        checkBox = itemView.findViewById(R.id.checkbox);
    }

    public void setDatas(List<LoveBean> loveBeans){

        this.loveBeans = loveBeans;
    }

    public CheckBox getCheckBox(){
        return checkBox;
    }

    public void setMode(boolean modle){

        this.modle = modle;
    }

    @Override
    public void setData(LoveBean item) {
        if(modle){
            checkBox.setVisibility(View.VISIBLE);
        }else {
            checkBox.setVisibility(View.GONE);
        }
        checkBox.setChecked(item.isChecked);
        title.setText(item.getName());

        int adapterPosition = getAdapterPosition();
        //时间
        if(adapterPosition>0){
            CharSequence timeSpanString= DateUtils.formatDateTime(JEApp.getInstance(),item.getSaveTime(), DateUtils.FORMAT_SHOW_YEAR);
            LoveBean historyBean = loveBeans.get(adapterPosition - 1);
            CharSequence preTimeSapn = DateUtils.formatDateTime(JEApp.getInstance(),historyBean.getSaveTime(), DateUtils.FORMAT_SHOW_YEAR);
            if(timeSpanString.equals(preTimeSapn)){
                dateTv.setVisibility(View.GONE);
            }else {
                dateTv.setText(timeSpanString);
                dateTv.setVisibility(View.VISIBLE);
            }
        }else if(adapterPosition==0){
            CharSequence timeSpanString = DateUtils.formatDateTime(JEApp.getInstance(),item.getSaveTime(), DateUtils.FORMAT_SHOW_YEAR);
            dateTv.setText(timeSpanString);
            dateTv.setVisibility(View.VISIBLE);
        }
    }
}
